package br.com.projetos.dao;

import java.security.NoSuchAlgorithmException;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import br.com.projetos.entity.Nomeado;

public class NomeadoDAO extends DAO {

	private static NomeadoDAO instance;

	private NomeadoDAO() {
	}

	public static NomeadoDAO getInstance() {
		if (instance == null) {
			instance = new NomeadoDAO();
		}
		return instance;
	}

	public void update(Object objeto) {
		try {
			// HibernateUtility.getSession().clear();
			HibernateUtility.beginTransaction();
			HibernateUtility.getSession().update(objeto);
			HibernateUtility.commitTransaction();
		} catch (Exception e) {
			HibernateUtility.rollbackTransaction();
		}
	}

	public Nomeado autentica(Nomeado nomeado) throws NoSuchAlgorithmException {
		// HibernateUtility.beginTransaction();
		Criteria c = HibernateUtility.getSession()
				.createCriteria(Nomeado.class);
		c.add(Restrictions.eq("cpf", nomeado.getCpf()));
		c.add(Restrictions.eq("senha", nomeado.getSenha()));
		// HibernateUtility.commitTransaction();
		return (Nomeado) c.uniqueResult();
	}

	public Nomeado recupera(Nomeado nomeado) {
		HibernateUtility.beginTransaction();
		Criteria c = HibernateUtility.getSession()
				.createCriteria(Nomeado.class);
		c.add(Restrictions.eq("siape", nomeado.getCpf()));
		HibernateUtility.commitTransaction();
		return (Nomeado) c.uniqueResult();
	}

}